package com.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.common.R;
import com.demo.dao.DishDao;
import com.demo.domain.Dish;
import com.demo.dto.DishDto;
import com.demo.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/dish")
@Slf4j
@Api(tags="菜品详情")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 分页查询菜品信息
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
//    @GetMapping("/page")
//    public R<Page> page(int page, int pageSize, String name){
//        log.info("前端传来的数据page为 {} pageSize为 {} name为{}",page,pageSize,name);
//        return dishService.page(page,pageSize,name);
//    }

    /**
     * 代码优化
     * 多表条件分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     */
    @GetMapping("/page")
    @ApiOperation("多表条件分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页记录数",required = true),
            @ApiImplicitParam(name = "name",value = "菜品名称",required = false)
    })
    public R<Page> pageByParams(int page, int pageSize, String name) {
        log.info("前端传来的数据page为 {} pageSize为 {} name为{}", page, pageSize, name);

        Page<DishDto> dishDtoPage = new Page<>(page, pageSize);
        DishDto dishDto = new DishDto();

        if (!StringUtils.isEmpty(name)) {
            dishDto.setName(name);
        }

        dishService.pageByParams(dishDtoPage, dishDto);

        return R.success(dishDtoPage);
    }


    /**
     * 添加菜品信息
     *
     * @param dishDto
     * @return
     */
    @PostMapping
    @ApiOperation(value = "添加菜品信息")
    public R<DishDto> dishsave(@RequestBody DishDto dishDto) {
        log.info("前端传来的信息dishDto为 {}", dishDto.toString());
        dishService.dishsave(dishDto);
        return R.success(dishDto);
    }


    /**
     * 单删除和批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "单删除和批量删除")
    public R<String> deleteById(Long[] ids) {
        log.info("前端传来的参数ids为 {}", ids);
        return dishService.deleteById(ids);
    }


    /**
     * 修改菜品起售与停售状态
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "修改菜品起售与停售状态")
    public R<String> updateStatus(@PathVariable int status, Long[] ids) {
        log.info("前端传来的参数status为 {} ids为 {}", status, ids);
        return dishService.updateStatus(status, ids);
    }


    /**
     * 根据id回显菜品基本信息和口味信息
     * @param id
     * @return
     */
//    @GetMapping("/{id}")
//    public R<DishDto> getDishWithFlavor(@PathVariable Long id){
//        log.info("前端传来的参数id为 {}" ,id);
//        return dishService.getDishWithFlavor(id);
//    }

    /**
     * 一对多查询  代码优化
     * 通过id回显数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过id回显数据")
    public R<DishDto> queryById(@PathVariable Long id) {
        DishDto dishDto = dishService.queryById(id);
        return R.success(dishDto);
    }


    /**
     * 修改菜品信息以及口味
     *
     * @param dishDto
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改菜品信息以及口味")
    public R<String> updateDishWithFlavor(@RequestBody DishDto dishDto) {
        log.info("前端传来的参数 dishDto为 {}", dishDto.toString());
        return dishService.updateDishWithFlavor(dishDto);
    }

    /**
     * 根据菜品类型id获取到对应的所有菜品
     * @param dish
     * @return
     */
//    @GetMapping("/list")
//    public R<List<DishDto>> queryDishList(Dish dish){
//        log.info("前端传来的参数dish为 {} ",dish);
//        return dishService.queryDishList(dish);
//    }

    /**
     * 根据菜品类型id获取到对应的所有菜品
     * 代码优化
     *
     * @param dish
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "根据菜品类型id获取到对应的所有菜品")
    public R<List<DishDto>> queryDishList(Dish dish) {
        log.info("前端传来的参数dish为 {} ", dish);

        List<DishDto> dishDaos = null;
        //设置Redis中的key为前端传来的参数
        String key = "dish_" + dish.getCategoryId() + "_" + dish.getStatus();

        //先获取到缓存Redis中的数据
        dishDaos = (List<DishDto>) redisTemplate.opsForValue().get(key);
        System.out.println("缓存中的数据为"+dishDaos);

        //如果缓存中有数据
        if(dishDaos != null){
            //将获取到的数据重新存入缓存Redis中
            return R.success(dishDaos);
        }

        dishDaos = dishService.DishList(dish);
//        System.out.println("=========="+dishDaos.toString());

        redisTemplate.opsForValue().set(key,dishDaos,60, TimeUnit.MINUTES);

        return R.success(dishDaos);
    }


}
